<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$return['express']=Array (
		'0' => Array ('id'=> 1, 'name' => '顺丰快递' ),
		'1' => Array ( 'id' => 2, 'name' => '申通快递' ) ,
		'2' => Array ( 'id' => 3,'name' => '韵达快运' ),
		'3' => Array ('id' => 4, 'name' => '天天快递' ),
		'4' => Array ( 'id' => 5, 'name' => '圆通速递' ) ,
		'5' => Array ( 'id' => 6,'name' => '中通速递' ) ,
		'6' => Array ( 'id' => 7 ,'name' => 'ems快递' ),
		'7' => Array ( 'id' => 8, 'name' => '汇通快运' ),
		'8' => Array ( 'id' => 9, 'name' => '其它快递' )
);
if($_RQ['op']=='main'){
	$return['post']=array();
	if($_RQ['ismygroup']){
		$this->session('ismygroup',intval($_RQ['ismygroup']));
		if($_RQ['ismygroup']==2){
			$return['nofahuo']=1;
		}
	}
	$return['status']=10;
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询进入该页面的客户所有订单
	$ismygroup=$this->session('ismygroup');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($ismygroup==1){
		$params['merchid']=$_SESSION['uid'];
	}else{
		$params['mid']=$_SESSION['uid'];
	}
	if(isset($_RQ['status']) && intval($_RQ['status'])!=10){
		$params['status']=intval($_RQ['status']);
		$return['status']=intval($_RQ['status']);
	}else{
		$return['status']=10;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_quan_feelog',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$info=pdo_get('jy_quan_item',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']));
			if($info['groupsendset']==1){
				$info['groupsendsettitle']='快递发货';
			}elseif($info['groupsendset']==0){
				$info['groupsendsettitle']='无需物流';
			}else{
				$info['groupsendsettitle']='自提点提货';
			}
			$formdata=pdo_get('jy_quan_item_formdata',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			if($formdata){
				$formdata['fileds']=json_decode($formdata['fileds'],true);
				foreach($formdata['fileds'] as $k=>&$fv){
					if($item['type']!=1 && $fv['total']<1){
						unset($formdata['fileds'][$k]);
					}else{
						/*foreach($fv as $fk=>&$f){
							//print_r($fv);exit;
							if($f['type']==1){
								$fv['title'] =$f['data'];
							}
							if($f['type']==18){
								$fv['fee'] =$f['data']*$fv['total'];
							}
							if($f['type']==3){
								$fv['image'] =$f['data'];
							}
						}*/
					}
				}
				$formdata['data']=json_decode($formdata['data'],true);
			}else{
				$orderinfo=pdo_get('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),array('addressid','id','sendtype'));
				$item['addressid']=$orderinfo['addressid'];
				$info['orderid']=$orderinfo['id'];
				$info['sendtype']=$orderinfo['sendtype'];
				if($orderinfo['id']){
					$info['groupsendsettitle']=$this->send_types[$orderinfo['sendtype']];
				}
			}
			$item['user']=$user;
			$item['info']=$info;
			$item['formdata']=$formdata;
			if($item['status']==0){
				$item['typestatus']='待支付';
			}
			if($item['status']==1){
				$item['typestatus']='已支付';
			}
			if($item['status']==2){
				$item['typestatus']='已发货';
			}
			if($item['status']>=3){
				$item['typestatus']=$item['status']==3?'已取消':'已完成';
			}
			if($item['status']>=2){
				$item['express']=pdo_get('jy_quan_order_express',array('order_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['express']['express_name']=$return['express'][$item['express']['express_id']-1]['name'];
			}
			$item['typetitle']=$this->itemtypes()[$info['type']];
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
		}
	}
	$return['list']=$list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_quan_feelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$item=pdo_get('jy_quan_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pid']));	
	//自定义字段数据
	$order['groupsendsettitle']=$item['groupsendset']==1?'快递发货':$item['groupsendset']==0?'无需物流':'自提点提货';
	$formdata=pdo_get('jy_quan_item_formdata',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	if($formdata){
		$formdata['fileds']=json_decode($formdata['fileds'],true);
		foreach($formdata['fileds'] as &$fv){
			foreach($fv as $fk=>&$f){
				if($f['type']==5 && $fv['data']){
					$f['data'] = explode(";", $f['data']);
					foreach($f['data'] as $k=>&$i){
						if(empty($f['data'][$k])){
							unset($f['data'][$k]);
						}
					}
				}
				if($f['type']==18){
						$fv['total'] = $post['type']!=1?1:$fv['total'];
						if($fv['options']){
							$post['fee'] +=$fv['options'][$fv['optionid']]['price']*$fv['total'];
							$f['data']=$fv['options'][$fv['optionid']]['price'];
						}else{
							$post['fee'] +=$f['data']*$fv['total'];
						}
				}
			}
			if($fv['options']){
				$fv[]=array('title'=>'所选规格','type'=>'option','data'=>$fv['options'][$fv['optionid']]['title']);
			}
		}
		$formdata['data']=json_decode($formdata['data'],true);
		foreach($formdata['data'] as $v){
			if($v['type']==7){
				$order['address']=$v['data']['province'].''.$v['data']['city'].''.$v['data']['district'].''.$v['data']['xiangxi'];
			}
			if($v['type']==8){
				$order['mobile']=$v['data'];
			}
			if($v['title']=='联系人'){
				$order['name']=$v['data'];
			}
		}
		$formdata['diydata']=json_decode($formdata['diydata'],true);
		foreach($formdata['diydata'] as &$v){
			if($v['type']==5 && $v['data']){
				$v['data'] = explode(";", $v['data']);
				foreach($v['data'] as $k=>&$i){
					if(empty($v['data'][$k])){
						unset($v['data'][$k]);
					}
				}
			}
		}
		$return['formdata']=$formdata;
	}else{
		$orderinfo=pdo_get('jy_weishop_order',array('ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']),array('addressid','id'));
		$address=pdo_get('core_address',array('id'=>$orderinfo['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$address['pcd']=json_decode($address['pcd'],true);
		$addressinfo=$address['pcd']['province'] . $address['pcd']['city'] . $address['pcd']['district'] . $address['pcd']['street'] . $address['address'];
		$order['address']=$addressinfo;
		$order['mobile']=$address['mobile'];
		$order['name']=$address['name'];
	}
	if($item['status']==0){
		$item['typestatus']='待支付';
	}
	if($item['status']==1){
		$item['typestatus']='已支付';
	}
	if($item['status']==2){
		$item['typestatus']='已发货';
	}
	if($order['status']>=3){
		$order['typestatus']=$order['status']==3?'已取消':'已完成';
	}
	if($order['status']>=2){
		$order['express']=pdo_get('jy_quan_order_express',array('order_id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		$order['express']['express_name']=$return['express'][$order['express']['express_id']-1]['name'];
	}
	$order['typetitle']=$this->itemtypes()[$order['type']];
	$order['createtime']=date('Y-m-d H:i:s', $order['createtime']);
	$order['sendtime']=date('Y-m-d H:i:s', $order['sendtime']);
	$order['paytime']=date('Y-m-d H:i:s', $order['paytime']);
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['item']=$item;
	$return['fileds']=$fileds;
	exi($return);
}
if($_RQ['op']=='fa'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$post['express_id'] || !$post['expresssn']){
			exi('请填写快递信息','error');
	}
	$data=array(
			'order_id'=>$id,
			'uniacid'=>$_SESSION['uniacid'],
			'express_id'=>$post['express_id'],
			'expresssn'=>$post['expresssn'],
			'createtime'=>time(),
	);
	$status=pdo_getcolumn('jy_quan_feelog',array('id'=>$id),'status');
	if($status!=1){
			exi('订单不是未发货状态','error');
	}
	pdo_insert('jy_quan_order_express',$data);
	pdo_update('jy_quan_feelog',array('status'=>2),array('id'=>$id));
	exi('已发货！','','main');
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_quan_feelog',array('status'=>'4'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('确认成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_quan_feelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功');
}